package singleton;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/22 21:37
 */


public class LazySingletonSafeByDbCheckTest {

    public static void main(String[] args) {

        new Thread(()->{
            System.out.println(LazySingletonSafeByDbCheck.getInstance());
        }).start();

        new Thread(()->{
            System.out.println(LazySingletonSafeByDbCheck.getInstance());
        }).start();

    }
}

//使用双重检查保证懒汉式的线程安全
class LazySingletonSafeByDbCheck{

    private static volatile LazySingletonSafeByDbCheck instance;

    //私有构造器
    private LazySingletonSafeByDbCheck(){

    }

    //提供一个公共访问节点，获得该实例对象
    public static  LazySingletonSafeByDbCheck getInstance(){

        if(instance == null){
            synchronized (LazySingletonSafeByDbCheck.class){
                if(instance == null){
                    instance = new LazySingletonSafeByDbCheck();
                }
                return instance;
            }
        }
        return instance;
    }
}